#define _CRT_SECURE_NO_WARNINGS 1

#include"Head.h"

//int main()
//{
//	//int a[] = { 50,100,70,65,60,32 };
//	int a[] = { 60,70,65,50,32,100 };
//	HeapSort(a, 6);
//	//Heap hp;
//	//HeapInit(&hp);
//	//for (int i = 0; i < sizeof(a) / sizeof(int); i++)
//	//{
//	//	HeapPush(&hp, a[i]);
//	//}
//
//
//	//while (!HeapEmpty(&hp))
//	//{
//	//	printf("%d\n", HeapTop(&hp));
//	//	HeapPop(&hp);
//	//}
//
//	//HeapDestory(&hp);
//
//	return 0;
//}


void GreateNDate()
{
	int i = 10000;
	srand(time(0));
	FILE* fin = fopen("date.txt", "w");
	if (fin == NULL)
	{
		perror("fopen error");
		return;
	}
	for (i; i >= 0; i--)
	{
		int x = rand();
		fprintf(fin, "%d\n", x);
	}
	fclose(fin);
}

void topk()
{
	int k;
	scanf("%d", &k);
	FILE* fin = fopen("date.txt", "r");
	if (fin == NULL)
	{
		perror("fopen error");
		return;
	}
	int* minheap = (int*)malloc(sizeof(int) * k);
	if (minheap == NULL)
	{
		perror("malloc fail");
		return;
	}
	int i = 0;
	for (i; i < k; i++)
	{
		fscanf(fin, "%d", &minheap[i]);
	}
	for (i = k - 1 - 1; i > 0; i--)
	{
		AdjustDown(minheap, k, i / 2);
	}
	int x = 0;
	while (fscanf(fin, "%d", &x) != EOF)
	{
		if (x > minheap[0])
		{
			minheap[0] = x;
			AdjustDown(minheap, k, 0);
		}
	}
	for (i = 0; i < k; i++)
	{
		printf("%d\t", minheap[i]);
	}
	fclose(fin);
}

int main()
{
	GreateNDate();
	topk();
	return 0;
}